package edu.umassd.raddacl.calc;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import edu.umassd.raddacl.DataSet;
import edu.umassd.raddacl.Observation;

/**
 * Metrics calculated on the dataset as a whole.
 * 
 * @author Dan Avila
 * 
 */
@Component
public class DataSetMetrics
{
	@Autowired
	private DataSet dataSet;

	@Autowired
	private DistanceTable table;

	private Double mean = 0.0;
	private Double variance = 0.0;
	private Double sqrt = 0.0;

	@PostConstruct
	protected void init()
	{
		dataSet.loop(new Mean());
		mean = mean / table.size();

		dataSet.loop(new Variance());
		sqrt = Math.sqrt(variance);
	}

	public Double getMean()
	{
		return mean;
	}

	public Double getVariance()
	{
		return variance;
	}

	public Double getStandardDev()
	{
		return sqrt;
	}

	private class Mean implements LoopAndCompareAlgorithm
	{
		@Override
		public void loop(Observation obs1, Observation obs2)
		{
			mean += table.getDistance(obs1, obs2);
		}
	}

	private class Variance implements LoopAndCompareAlgorithm
	{
		@Override
		public void loop(Observation obs1, Observation obs2)
		{
			Double dist = table.getDistance(obs1, obs2);

			variance += Math.pow(dist - mean, 2.0);
		}
	}
}
